package org.example;


// import java.util.ArrayList;
// import java.util.List;

class AIPlayer extends Player {

    private static final int EASY_DEPTH = 1; // 简单模式搜索深度
    private static final int HARD_DEPTH = 3; // 困难模式搜索深度
    private int depth;

    public AIPlayer(int color, boolean isHardMode) {
        super(color);
        this.depth = isHardMode ? HARD_DEPTH : EASY_DEPTH;
        // // 创建简单模式的 AI 玩家
        // AIPlayer easyAI = new AIPlayer(1, false);
        // // 创建困难模式的 AI 玩家
        // AIPlayer hardAI = new AIPlayer(2, true);
    }

    // 获取最佳落子点
    public Point getBestMove(Border border) {
        int bestScore = Integer.MIN_VALUE;
        Point bestMove = null;
        int alpha = Integer.MIN_VALUE;
        int beta = Integer.MAX_VALUE;

        // 遍历棋盘上所有可能的落子点
        for (int x = 0; x < border.getSize(); x++) {
            for (int y = 0; y < border.getSize(); y++) {
                if (border.checkMove(x, y)) {
                    // 模拟落子
                    border.move(x, y, this);
                    // 调用 minimax 算法计算得分
                    int score = minimax(border, depth - 1, false, alpha, beta);
                    // 撤销落子
                    border.undoMove(x, y);

                    if (score > bestScore) {
                        bestScore = score;
                        bestMove = new Point(x, y);
                    }
                    alpha = Math.max(alpha, bestScore);
                }
            }
        }
        return bestMove;
    }

    // Minimax 算法结合 Alpha-Beta 剪枝
    private int minimax(Border border, int depth, boolean isMaximizing, int alpha, int beta) {
        if (depth == 0 || border.isGameOver()) {
            return evaluate(border);
        }

        if (isMaximizing) {
            int maxEval = Integer.MIN_VALUE;
            for (int x = 0; x < border.getSize(); x++) {
                for (int y = 0; y < border.getSize(); y++) {
                    if (border.checkMove(x, y)) {
                        border.move(x, y, this);
                        int eval = minimax(border, depth - 1, false, alpha, beta);
                        border.undoMove(x, y);
                        maxEval = Math.max(maxEval, eval);
                        alpha = Math.max(alpha, eval);
                        if (beta <= alpha) {
                            break;
                        }
                    }
                }
            }
            return maxEval;
        } else {
            int minEval = Integer.MAX_VALUE;
            Player opponent = new HPlayer(3 - this.getColor()); // 假设对手是人类玩家
            for (int x = 0; x < border.getSize(); x++) {
                for (int y = 0; y < border.getSize(); y++) {
                    if (border.checkMove(x, y)) {
                        border.move(x, y, opponent);
                        int eval = minimax(border, depth - 1, true, alpha, beta);
                        border.undoMove(x, y);
                        minEval = Math.min(minEval, eval);
                        beta = Math.min(beta, eval);
                        if (beta <= alpha) {
                            break;
                        }
                    }
                }
            }
            return minEval;
        }
    }

    // 评估函数，简单示例，可根据实际情况调整
    private int evaluate(Border border) {
        int score = 0;
        for (Point p : this.getChessList()) {
            if (border.isWin(p.getX(), p.getY(), this)) {
                score += 1000;
            }
        }
        Player opponent = new HPlayer(3 - this.getColor());
        for (Point p : opponent.getChessList()) {
            if (border.isWin(p.getX(), p.getY(), opponent)) {
                score -= 1000;
            }
        }
        return score;
    }
}
